package example.hellosecurity.config;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
* 自定义登录成功后的处理器
* 转发或者重定向，由代码逻辑实现
*
* */

public class MyAuthenticationSuessHandler implements AuthenticationSuccessHandler {

    private String url;
    private  boolean isRedirect;

    public  MyAuthenticationSuessHandler(String url,boolean isRedirect){
        this.url=url;
        this.isRedirect=isRedirect;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {

        /*
        * request 请求对象request.getRequestDispatcher(url).forward(request,response);
        * response  响应对象response.sendRedirect(url);
        * authentication 用户认证（）成功登录后的对象，其中包含用户名和权限集合。内容是，自定义的UserDetailService的实现方法值对象提供的
        * */
        System.out.println(" 自定义登录成功后处理器执行了");

if (isRedirect){
    response.sendRedirect(url);
}else {
    request.getRequestDispatcher(url).forward(request,response);
}


    }
}
